#!/python/env/python3.5/bin/python
# coding:utf-8
import time, os, json, re, random
import requests

class Alimama():
    def __init__(self):
        print('开始运行')

    def searchByTbid(self, tbid):
        ms = int(round(time.time() * 1000))
        url = 'http://pub.alimama.com/items/search.json'
        params = {
            'q':'https://detail.tmall.com/item.htm?id='+tbid,
            '_t':ms,
            'perPageSize':1,
            'shopTag':'dpyhq',
            't':ms,
            '_tb_token_':'ea17be8b6a46d',
            'dpyhq': 1
        }
        res = requests.get(url, params)
        if res.status_code == 200 :
            data = res.json()
            if data['data']['pageList'] :
                coupon = data['data']['pageList'][0]
                if coupon['couponAmount'] :
                    return {
                        'quan_fee' : coupon['couponAmount'],
                        'sales' : coupon['biz30day'],
                        'taobao_title' : coupon['title'],
                        'tbid' : coupon['auctionId'],
                        'commission_rate' : coupon['tkRate']
                    }
            return false

if __name__ == '__main__':
    obj = Alimama()
    res = obj.searchByTbid('564722884180')
    print(res)
